package com.songlanyun.modules.saleconfig.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.drive.Validator;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.BeanUtil;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.common.validator.group.AddGroup;
import com.songlanyun.common.validator.group.UpdateGroup;
import com.songlanyun.modules.exception.ConfigException;
import com.songlanyun.modules.pointsexchangeconfig.entity.PointsExchangeConfigEntity;
import com.songlanyun.modules.pointsexchangeconfig.service.PointsExchangeConfigService;
import com.songlanyun.modules.saleconfig.model.dto.SaleConfigDTO;
import com.songlanyun.modules.saleconfig.model.vo.SaleConfigVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.saleconfig.dao.SaleConfigDao;
import com.songlanyun.modules.saleconfig.entity.SaleConfig;
import com.songlanyun.modules.saleconfig.service.SaleConfigService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("saleConfigService")
public class SaleConfigServiceImpl extends ServiceImpl<SaleConfigDao, SaleConfig> implements SaleConfigService {


    @Resource
    private PointsExchangeConfigService pointsExchangeConfigService;

    /**
     * 设置平台交易配置
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void set(SaleConfigDTO dto) {
        ValidatorUtils.validateEntity(dto);

        //保存平台交易配置
        SaleConfig saleConfig = new SaleConfig();
        BeanUtil.copyPropertiesIgnoreNull(dto, saleConfig);
        add(saleConfig);

        //保存积分兑换配置
        PointsExchangeConfigEntity pointsExchangeConfig = new PointsExchangeConfigEntity();
        BeanUtil.copyPropertiesIgnoreNull(dto, pointsExchangeConfig);
        pointsExchangeConfigService.add(pointsExchangeConfig);
    }

    private void add(SaleConfig saleConfig) {
        SaleConfig config = this.baseMapper.selectFirst();
        if (config == null) {
            this.baseMapper.insert(saleConfig);
//            return saleConfig;
        } else {
            Integer id = config.getId();
            BeanUtils.copyProperties(saleConfig, config);
            config.setId(id);
            this.baseMapper.updateById(config);
//            return config;
        }
    }

    @Override
    public SaleConfig getConfig() {
        return this.baseMapper.selectOne(
                new LambdaQueryWrapper<SaleConfig>()
                        .last("limit 1")
        );
    }

    @Override
    public SaleConfig getConfig(Boolean tw) {
        SaleConfig config = this.getConfig();
        if (config == null) {
            if (tw) {
                throw new RRException(ConfigException.SALE_CONFIG_NOT_EXIST);
            }
        }
        return config;
    }

    @Override
    public long getExpiredTimestamp() {
        SaleConfig config = this.getConfig(true);
        /**
         * 计算支付过期的时间
         */
        if (config != null) {
            return config.getPaymentDelayConfig().getExpiredTimestamp(System.currentTimeMillis());
        } else {
            /**
             * 默认支付过期两个小时
             */
            return System.currentTimeMillis() + 2 * 60 * 60 * 1000;
        }
    }

    @Override
    public SaleConfigVO info() {
        SaleConfigVO vo = new SaleConfigVO();

        //平台交易配置
        SaleConfig config = this.getConfig();
        if (ObjectUtil.isNotNull(config)) {
            BeanUtil.copyPropertiesIgnoreNull(config, vo);
        }

        //积分兑换配置
        PointsExchangeConfigEntity pointsExchangeConfig = pointsExchangeConfigService.loadConfig(false);
        if (ObjectUtil.isNotNull(pointsExchangeConfig)) {
            BeanUtil.copyPropertiesIgnoreNull(pointsExchangeConfig, vo);
        }
        return vo;
    }
}
